/**
 * 
 */
package com.lys.bo;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import com.lys.po.Account;
import com.lys.po.FullName;

/**
 * @author lys
 * 
 */
public class AccountTransfor {

	public AccountBO toBO(Account account) {
		AccountBO accountBO = null;
		if (account != null) {
			accountBO = new AccountBO();
			BeanUtils.copyProperties(account, accountBO, new String[] { "fullname" });
			FullName fullname = account.getFullname();
			if (fullname != null) {
				accountBO.setFullname(fullname.toString());
			}
		}
		return accountBO;
	}
	
	public List<AccountBO> toBO(List<Account> accounts) {
		List<AccountBO> accountBOs = null;
		if(!CollectionUtils.isEmpty(accounts)){
			accountBOs = new ArrayList<AccountBO>(accounts.size());
			for (Account account: accounts) {
				accountBOs.add(toBO(account));
			}
		}
		return accountBOs;
	}
	
	public Account toPO(AccountBO accountBO) {
		Account account = null;
		if (accountBO != null) {
			account = new Account();
			BeanUtils.copyProperties(accountBO, account, new String[] { "fullname" });
			if (!StringUtils.isEmpty(accountBO.getFirstname()) || !StringUtils.isEmpty(accountBO.getLastname())) {
				FullName fullname = new FullName(accountBO.getFirstname(), accountBO.getLastname());
				account.setFullname(fullname);
			}
		}
		return account;
	}
	
	public List<Account> toPO(List<AccountBO> accountBOs) {
		List<Account> accounts = null;
		if(!CollectionUtils.isEmpty(accounts)){
			accounts = new ArrayList<Account>(accountBOs.size());
			for (AccountBO accountBO: accountBOs) {
				accounts.add(toPO(accountBO));
			}
		}
		return accounts;
	}

}
